package v1

import (
	"bc/service"
	"bc/util"
	"crypto/md5"
	"encoding/hex"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
	"unicode/utf8"
)
type user struct {

}
func (u *user)Create(c *gin.Context){

	d := make(map[string]interface{})

	if v,ok := c.GetPostForm("rid");ok{
		rid,_ := strconv.Atoi(v)
		d["rid"] = rid
	}else{
		d["rid"] = 0
	}
	if v,ok := c.GetPostForm("dp_id");ok{
		dp_id , err := strconv.Atoi(v)
		if err != nil{
			c.JSON(200,gin.H{
				"code":500,
				"msg":"部门错误，请重试",
			})
			return
		}
		d["dp_id"] = dp_id
	}

	if v,ok := c.GetPostForm("username");ok{

		user := service.User.First(map[string]interface{}{"username":v})
		if user != nil{
			c.JSON(200,gin.H{
				"code":500,
				"msg":"用户已存在",
			})
			return
		}
		d["username"] = v
	}

	if v,ok := c.GetPostForm("nickname");ok{
		d["nickname"] = v
	}
	if v,ok := c.GetPostForm("desc");ok{
		d["desc"] = v
	}
	if v,ok := c.GetPostForm("password");ok{

		md5Pwd := md5.New()
		md5Pwd.Write([]byte(util.Config.App.Salt + v))
		pwd := hex.EncodeToString(md5Pwd.Sum(nil))

		d["password"] = pwd
	}

	if v,ok := c.GetPostForm("status");ok{
		status , _ := strconv.Atoi(v)

		d["status"] = status
	}else{
		d["status"] = 1
	}

	d["c_time"] = time.Now().Unix()

	b,last_id := service.User.Create(d)
	if b == true{
		LogAdd(c,"添加用户" + strconv.Itoa(last_id),"权限用户")
		c.JSON(200,gin.H{
			"code":200,
			"msg":"操作成功",
		})
		return
	}
	c.JSON(200,gin.H{
		"code":500,
		"msg":"error",
	})

}
func (u *user) Del(c *gin.Context){

	mUid := c.Param("uid")

	if strToInt,_ := strconv.Atoi(mUid);strToInt == 1{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"no permise",
		})
		return
	}

	m := map[string]interface{}{
		"uid":mUid,
	}

	bool := service.User.Del(m)
	if bool == true{
		LogAdd(c,"删除用户" + mUid,"权限用户")
		/*
		con := "删除用户" + mUid
		LogAdd(c,con)*/
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"删除成功",
		"data":"删除成功",
	})
}

func (u *user) Update (c *gin.Context){

	uid := c.Param("uid")
	if uid == ""{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"参数有误",
		})
		return
	}
	user := service.User.First(map[string]interface{}{"uid":uid})
	if user == nil{
		c.JSON(200,gin.H{
			"code":200,
			"msg":"用户不存在",
			"data":"用户不存在",
		})
		return
	}

	w := map[string]interface{}{
		"uid":uid,
	}

	f := make(map[string]interface{})
	if v,ok := c.GetPostForm("rid");ok{
		rid ,err := strconv.Atoi(v)
		if err != nil{
			c.JSON(200,gin.H{
				"code":500,
				"msg":"数据有误，请重试",
				"data":"数据有误，请重试",
			})
			return
		}
		f["rid"] = rid
	}

	if v,ok := c.GetPostForm("dp_id");ok{
		dp_id,err := strconv.Atoi(v)
		if err != nil {
			c.JSON(200,gin.H{
				"code":500,
				"msg":"数据有误，请重试",
				"data":"数据有误，请重试",
			})
			return
		}
		f["dp_id"] = dp_id
	}
	if v,ok := c.GetPostForm("username");ok{
		f["username"] = v
	}
	if v,ok := c.GetPostForm("nickname");ok{
		f["nickname"] = v
	}
	if v,ok := c.GetPostForm("desc");ok{
		f["desc"] = v
	}

	if v,ok := c.GetPostForm("password");ok{

		md5Pwd := md5.New()
		md5Pwd.Write([]byte(util.Config.App.Salt + v))
		pwd := hex.EncodeToString(md5Pwd.Sum(nil))
		if pwd != user.Password{
			c.JSON(200,gin.H{
				"code":500,
				"msg":"error",
				"data":"原始密码错误",
			})
			return
		}
		//f["password"] = pwd



	}

	old_pwd,ook := c.GetPostForm("old_password")
	cim_pwd,cok := c.GetPostForm("confirm_pwd")
	if ook && cok{
		if old_pwd != cim_pwd{
			c.JSON(200,gin.H{
				"code":500,
				"msg":"error",
				"data":"两次确认密码不一致",
			})
			return
		}
		msg := ""

		if utf8.RuneCountInString(old_pwd) < 6 {
			msg = "密码太弱,建议长度大于等于六位"
		}
		if utf8.RuneCountInString(old_pwd) > 16 {
			msg = "密码太长了,建议长度小于十五位"
		}

		if msg != "" {
			c.JSON(200,gin.H{
				"code":500,
				"msg":"error",
				"data":msg,
			})
			return
		}

		md5Pwd := md5.New()
		md5Pwd.Write([]byte(util.Config.App.Salt + old_pwd))
		pwd := hex.EncodeToString(md5Pwd.Sum(nil))
		f["password"] = pwd
	}
	if v,ok := c.GetPostForm("status");ok{
		status,_ := strconv.Atoi(v)
		f["status"] = status
	}else{
		f["status"] = 1
	}

	b := service.User.Update(w,f)
	if b == true{
		LogAdd(c,"更新角色" + uid,"权限用户")
		c.JSON(200,gin.H{
			"code":200,
			"msg":"修改成功",
		})
		return
	}
	c.JSON(200,gin.H{
		"code":500,
		"msg":"error",
	})
}
func (u *user) First(c *gin.Context){

	w := make(map[string]interface{})
	w["uid"] = c.Param("uid")

	d := service.User.First(w)
	if d == nil{
		c.JSON(200,gin.H{
			"code":200,
			"msg":"操作成功",
			"data":"",
		})
		return
	}
	//d.FormatTime = time.Unix(d.Ctime,0).Format("2006-01-02 15:04:05")

	bt,_ := json.Marshal(d)

	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":string(bt),
	})

}
func (u *user) Row(c *gin.Context){

	m := map[string]interface{}{}
	page,_ := strconv.Atoi(c.DefaultQuery("page","1"))
	pageSize := c.DefaultQuery("pageSize",strconv.FormatInt(util.Config.App.PageSize,10))
	NpageSize,_ := strconv.Atoi(pageSize)

	data,t := service.User.Row(m,page,NpageSize)

	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":data,
		"total":t,
	})

}
var User = &user{}
